package cn.com.dstz.admin.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import cn.com.dstz.agent.config.AnalysisConfig;
import cn.com.dstz.base.action.BaseAction;
import cn.com.dstz.data.post.AnalysisKeyVal;
import cn.com.dstz.data.post.IP_PropertiesLoad;
import cn.com.dstz.data.post.Post;
import cn.com.dstz.data.post.PostTask;
import cn.com.dstz.utils.ActionUtils;
import cn.com.dstz.utils.DataBaseUtils;
import cn.com.dstz.utils.DateUtils;
import cn.com.dstz.utils.ValidUtils;
import cn.com.dstz.web.constants.Constants;

/**
 * 分析管理Action
 * @author 叶装装
 * 2013-5-23 10:43 修复
 * 二次修改: 2013-06-04 19:30
 */
public class AnalysisAct extends BaseAction{
	
	
	public void list( ){
		
		Map< String, Object > admin = this.getEntityInSession( Constants.ADMIN_IN_SESSION );
		int level = ActionUtils.getAdminLevel( admin );
		
		String sql = "select f.*, a.username from dstzfx f, dstzadmin a where f.adminid = a.id";
		Map< String, Object > valueMap = null;
		
		if( level > 0 ){
			AnalysisConfig config = new AnalysisConfig( admin, this.countManagerGroup( ) );
			sql += config.getSQL( );
			valueMap = config.getValueMap( );
		}
	
		this.responseToJsonObject( this.paginationBySQL( sql, null, valueMap,"f.sendtime" )  );
		return;
	}
	
	public void add( ){
		int validSaveOrUpdate = this.validSaveOrUpdate( 0 );
		
		if( validSaveOrUpdate > 0 ){
			this.ajaxResponse( validSaveOrUpdate );
			return;
		}
		
		Map< String, Object > admin = this.getEntityInSession( Constants.ADMIN_IN_SESSION );
		
		int save = baseService.save( "dstzfx", DataBaseUtils.getData( new String[]{
				"aid", "adminid", "title", "content", "sendtime"
		}, new Object[]{
				admin.get( "topaid" ), admin.get( "id" ), title, content, DateUtils.getCurrentTime( )
		} ) );
		
		this.addLog( save, "analysis", 0,  title );
		
		
		if( save > 0 ){
			Post post = new Post( IP_PropertiesLoad.getInstance( ).getIps( ), new AnalysisKeyVal( admin.get( "topaid" ), title, content ));
			postTask.addTask( post );
		}
		
		this.ajaxResponse( save > 0 ? 0 : -1 );
		return;
		
	}
	
	public void edit( ){
		
		int validSaveOrUpdate = validSaveOrUpdate( 1 );
		
		if( validSaveOrUpdate > 0 ){
			this.ajaxResponse( validSaveOrUpdate );
			return;
		}
		
		Map< String, Object > admin = this.getEntityInSession( Constants.ADMIN_IN_SESSION );
		int level = ActionUtils.getAdminLevel( admin );
		
		Map< String, Object > whereData = new HashMap< String, Object >( );
		
		whereData.put( "id", id );
		
		if( level > 0 ){
			whereData.put( "aid", admin.get( "aid" ) );
		}
		
		int update = baseService.update( "dstzfx", DataBaseUtils.getData( new String[]{
				"title", "content"
		}, new Object[]{
				title, content
		} ), whereData );
		
		this.addLog( update, "analysis", 1, "ID：" + id );
		
		int response =  update > 0 ? 0 : -1;
		
		if( level > 0 && response == -1 ){
			response = -99;
		}
		
		this.ajaxResponse( response );
		
		return;
		
	}
	
	public void delete( ){
		
		String ids = getParameter( "ids" );
		
		if( ids != null ){
			String [] splitIds = StringUtils.split( ids, "," );
			
			if( splitIds != null && splitIds.length > 0 ){
				List< String > updateSqlList = new ArrayList< String >( );
				
				Map< String, Object > admin = this.getEntityInSession( Constants.ADMIN_IN_SESSION );
				int level = ActionUtils.getAdminLevel( admin );
				
				for( String id : splitIds ){
					if( StringUtils.isNotBlank( id )){
						String delSql = "delete from dstzfx where id=" + id;
						if( level > 0 ){
							delSql += " and adminid=" + admin.get( "id" );
						}
						
						updateSqlList.add(  delSql );
					}
				}
				
				int del = baseService.saveOrUpdateBatch( updateSqlList );
				
				this.addLog( del, "notice", 2, ids );
			}
		}
		
	}
	
	public String toadd( ){
		
		return "add";
	}
	
	public String toedit( ){
		
		int validSaveOrUpdate = validSaveOrUpdate( 2 );
		
		if( validSaveOrUpdate > 0 ){
			this.alertMsg( null );
			return "edit";
		}
		
		String sql = "select f.*, a.username from dstzfx f, dstzadmin a where f.adminid = a.id and f.id=:id";
		Map< String, Object > valueMap = new HashMap< String, Object >( );
		valueMap.put( "id", id );
		Map< String, Object > data = baseService.queryForMap( sql, valueMap );
		getRequest( ).setAttribute( "data", data );
		
		return "edit";
		
	}
	public String toView(){
		String sql = "select f.*, a.username from dstzfx f, dstzadmin a where f.adminid = a.id and f.id=:id";
		Map< String, Object > valueMap = new HashMap< String, Object >( );
		valueMap.put( "id", id );
		Map< String, Object > data = baseService.queryForMap( sql, valueMap );
		getRequest( ).setAttribute( "data", data );
		return "toview";
	}
	private int validSaveOrUpdate( int saveOrUpdate ){
		if( saveOrUpdate == 2 || saveOrUpdate == 1 ){
			if( id == null || id == 0 ){
				return 3;
			}
			if( saveOrUpdate == 2 ){
				return 0;
			}
		}
		if( ValidUtils.validLength( title, 1, 60 )){
			return 1;
		}
		
		if( ValidUtils.validLength( content, 5, 30000 )){
			return 2;
		}
		
		return 0;
		
	}
	
	private String title;
	private String content;
	private Integer id;
	
	public String getTitle( ) {
		return title;
	}

	public void setTitle( String title ) {
		if( StringUtils.isNotBlank( title )){
			title = decode( title );
		}
		this.title = title;
	}

	public String getContent( ) {
		return content;
	}

	public void setContent( String content ) {
		if( StringUtils.isNotBlank( content )){
			content = decode( content );
		}
		this.content = content;
	}

	public Integer getId( ) {
		return id;
	}

	public void setId( Integer id ) {
		this.id = id;
	}

	private PostTask postTask;

	public PostTask getPostTask( ) {
		return postTask;
	}

	public void setPostTask( PostTask postTask ) {
		this.postTask = postTask;
	}


	private static final long serialVersionUID = 1L;

}
